package com.lemon.smartsafety.ui.activity.notice;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuItem;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.common.PhoneCallEvent;
import com.lemon.smartsafety.http.service.NoticeServices;
import com.lemon.smartsafetynew.ui.activity.base.BaseSearchFragmentActivity;
import com.lemon.smartsafetynew.ui.activity.base.LemonPermissionListener;
import com.lemon.smartsafety.ui.fragment.notice.NoticeSentDetailListFragment;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

/**
 * Created by Cooliean on 16/4/8.
 */
public class NoticeSentDetailListActivity extends BaseSearchFragmentActivity {

    private NoticeSentDetailListFragment mFragment;
    private String                  mKeyWord  = "";
    private String                  sNoticeId = "";
    private NoticeServices.ReadType mReadType = NoticeServices.ReadType.UN_READ;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            sNoticeId = getIntent().getStringExtra(BundleKey.NOTICE_ID);
        }
        sNoticeId = StringUtils.nullValue(sNoticeId);
    }


    @Override
    protected void initViews() {
        super.initViews();
        if (sNoticeId.length() == 0) {
            mHUDUtil.showErrorTip(getString(R.string.params_error), new HUDUtil.CallBack() {
                @Override
                public void onCall() {
                    callFinish();
                }
            });
        }
    }

    @Override
    public Fragment getFragment() {
        mFragment = NoticeSentDetailListFragment.newInstance(mKeyWord, mReadType, sNoticeId);
        return mFragment;
    }


    public Fragment getFragment(String text, NoticeServices.ReadType type) {
        mFragment = NoticeSentDetailListFragment.newInstance(text, type, sNoticeId);
        return mFragment;
    }


    @Override
    public int getSearchHintRes() {
        return R.string.search_common_hit;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean menuResult = super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_notice_reader_detail, menu);
        return menuResult;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        OkHttpUtils.getInstance().cancelTag(EventTag.TAG_NOTICE_SENT_DETAIL_LIST);//去掉之前的请求
        if (item.getItemId() == R.id.action_read) {
            mReadType = NoticeServices.ReadType.READ;
            setBarTitle("阅读人员详情" + "-" + "已阅");
        } else if (item.getItemId() == R.id.action_un_read) {
            mReadType = NoticeServices.ReadType.UN_READ;
            setBarTitle("阅读人员详情" + "-" + "未阅");
        }
        if (item.getItemId() == R.id.action_read || item.getItemId() == R.id.action_un_read) {
            mKeyWord = "";
            showSearch(false);
            replaceFragment(R.id.main_content, getFragment(mKeyWord, mReadType));
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onQueryTextSubmit(String queryText) {
        OkHttpUtils.getInstance().cancelTag(EventTag.TAG_NOTICE_SENT_DETAIL_LIST);//去掉之前的请求
        mKeyWord = StringUtils.nullValue(queryText);
        replaceFragment(R.id.main_content, getFragment(mKeyWord, mReadType));
        return super.onQueryTextSubmit(queryText);
    }

    @Override
    protected void initTitle() {
        setBarTitle("阅读人员详情" + "-" + "未阅");
    }


    @Subscribe
    public void onEventMainThread(final PhoneCallEvent event) {
        if (event.getTag().equals(NoticeSentDetailListFragment.class.getName())) {
            getConfirmDialog("是否拨打电话：" + event.getPhone(), new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        showPhoneCall(StringUtils.nullValue(event.getPhone()));
                    }
                }
            }).show();

        }
    }

    private void showPhoneCall(final String phone){
        if (phone.length() == 0) {
            return;
        }
        checkPermission(getString(R.string.permission_call), new LemonPermissionListener() {
            @Override
            public void onPermissionGranted(int requestCode) {
                Intent phoneIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                //@SuppressWarnings("unchecked")
                NoticeSentDetailListActivity.this.startActivity(phoneIntent);
            }

            @Override
            public void onPermissionDenied(int requestCode,ArrayList<String> deniedPermissions) {
                mHUDUtil.showErrorTip(getString(R.string.permission_denied_action));
            }
        },Manifest.permission.CALL_PHONE);
    }

}
